<template>
  <div class="app-container">
    <el-dialog :title="titleList[flag]+'（'+typeList[productType]+'）'" :visible.sync="dialogVisible" width="60%">
      <div class="myTitle">基本信息</div>
      <el-form ref="form" :model="form" :disabled="flag==1" v-loading="loading">
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'名称'" prop="productName" required>
              <div v-if="flag==1">{{ form.productName }}</div>
              <el-input v-else v-model="form.productName" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'编码'" prop="productCode" required>
              <div v-if="flag==1">{{ form.productCode }}</div>
              <el-input v-else v-model="form.productCode" placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'来源'" prop="productSource" required>
              <dict-tag v-if="flag==1" :options="dict.type.basic_good_source" :value="form.productSource"/>
              <el-select v-else v-model="form.productSource" filterable style="width: 100%">
                <el-option
                  v-for="item in dict.type.basic_good_source"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'类型'" prop="productCategoryId" required>
              <div v-if="flag==1">{{ form.productCategoryName }}</div>
              <treeselect v-else v-model="form.productCategoryId" :options="goodTypeList" :show-count="true"
                          placeholder="请选择" :normalizer="normalizer" :disabled="flag==1"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'品牌'" prop="brandId">
              <div v-if="flag==1">{{ form.brandName }}</div>
              <el-select v-else v-model="form.brandId" filterable clearable style="width: 100%">
                <el-option
                  v-for="item in brandList"
                  :key="item.id"
                  :label="item.brandName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单位" prop="unit">
              <div v-if="flag==1">{{ form.unit }}</div>
              <el-input v-else v-model="form.unit" placeholder="请输入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="8">
            <el-form-item :label="typeList[productType]+'图片'" prop="imageUrl" required>
              <div v-if="flag==1">
                <div class="myImg" v-if="form.imageUrl">
                  <el-image
                    :src="form.imageUrl"
                    fit="contain"
                    :preview-src-list="[form.imageUrl]"
                  >
                  </el-image>
                </div>
                <div v-else>暂无</div>
              </div>
              <div v-else>
                <div class="myImg" v-if="form.imageUrl">
                  <i class="el-icon-close" @click="form.imageUrl=null"></i>
                  <el-image
                    :src="form.imageUrl"
                    fit="contain"
                    :preview-src-list="[form.imageUrl]"
                  >
                  </el-image>
                </div>
                <el-upload
                  v-else
                  :headers="headers"
                  :action="action"
                  :on-success="uploadSuccess"
                  :on-progress="uploadProgress"
                  :show-file-list="false"
                  accept=".jpg,.jpeg,.png"
                >
                  <el-button type="primary" plain>点击上传</el-button>
                </el-upload>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogOk" v-if="flag!=1">确 定</el-button>
      </div>
    </el-dialog>
    <tempDialog ref="tempDialog"></tempDialog>
  </div>
</template>

<script>
import {addGood, listBrand, listGoodSku, updateGood} from "../../../api/imsMng";
import tempDialog from "../../../myComp/tempDialog/tempDialog.vue";
import {getToken} from '@/utils/auth'

import Treeselect from "@riophae/vue-treeselect";
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  props: ['goodTypeList'],
  dicts: ['basic_good_source'],
  components: {Treeselect, tempDialog},
  data() {
    return {
      flag: 0,
      productType: 0,
      loading: false,
      dialogVisible: false,
      list: [],
      open: false,
      form: {},
      titleList: ['新增', '详情', '修改'],
      typeList: ['', '商品', '配件', '物料'],
      brandList: [],
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      action: process.env.VUE_APP_BASE_API + '/ims/product/image/upload',
      skuList: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.productCategoryName,
          children: node.productCategoryVoList,
        }
      },
    }
  },
  methods: {
    show(flag, productType, form) {
      this.dialogVisible = true
      this.flag = flag
      this.productType = productType
      this.resetForm('form')
      this.skuList = []
      this.form = form
      if (flag) {
        this.load(form.id)
      }
      listBrand().then(res => {
        this.brandList = res.rows
      })
    },
    load(productId) {
      listGoodSku({productId: productId}).then(res => {
        this.skuList = res.data
      })
    },
    uploadProgress() {
      this.loading = true
    },
    uploadSuccess(res) {
      this.loading = false
      if (res.code == 200) {
        this.$set(this.form, 'imageUrl', res.data.imageUrl)
      } else {
        this.$message.error(res.msg)
      }
    },
    dialogOk() {
      this.$refs.form.validate(e => {
        if (e) {
          this.form.productType = this.productType
          this.loading = true
          if (this.flag) {
            updateGood(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('修改成功')
            }).finally(e => {
              this.loading = false
            })
          } else {
            addGood(this.form).then(res => {
              this.$emit('ok')
              this.dialogVisible = false
              this.$message.success('新增成功')
            }).finally(e => {
              this.loading = false
            })
          }
        }
      })
    },
  }
}
</script>
